<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>Document</title>
</head>
<body style="height: 3000px">
<div id="app">
    <button @click="isShow=!isShow">{{isShow ? '隐藏' : '显示'}}</button>
    <common-head v-if="isShow"></common-head>
</div>
<script src="./vue.js"></script>
<script>
    const CommonHead = {
        template: `
          <div>
          <h2>这是一个公共头部</h2>
          </div>
        `,
        data() {
            return {
                timer: null
            }
        },
        mounted() {
            this.timer = setInterval(() => {
                console.log(1);
            }, 1000)
            window.onscroll = function () {
                console.log('页面滚动了');
            }
        },
        beforeDestroy() {
            clearInterval(this.timer);
            window.onscroll = null;
        }

    }
    Vue.component('CommonHead', CommonHead);
    const vm = new Vue({
        el: '#app',
        data: {
            isShow: true
        }
    })
</script>
</body>
</html>